package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Slf4j
@Api(tags = "分类管理相关接口")
public class CategoryController {

    //注入service对象
    @Autowired
    private CategoryService categoryService;


    /**
     * 分类分页查询
     */
    @ApiOperation(value = "分类分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("分类分页查询,{}",categoryPageQueryDTO);

        //调用service层进行分页查询
        PageResult pageResult = categoryService.pageQuery(categoryPageQueryDTO);

        //返回结果给前端
        return Result.success(pageResult);
    }

    /**
     * 新增分类
     * @param categoryDTO
     * @return
     */
    @ApiOperation(value = "新增分类")
    @PostMapping()
    public Result save(@RequestBody CategoryDTO categoryDTO){
        log.info("新增分类,{}",categoryDTO);

        //调用service层进行新增
        categoryService.save(categoryDTO);

        //返回结果给前端
        return Result.success();
    }

    /**
     * 根据id删除分类
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id删除分类")
    @DeleteMapping()
    public Result deleteById(Long id){
        log.info("根据id删除分类,{}",id);

        //调用service层进行删除
        categoryService.deleteById(id);

        //返回结果给前端
        return Result.success();
    }

    /**
     * 修改分类
     * @return
     */
    @ApiOperation(value = "修改分类")
    @PutMapping()
    public Result update(@RequestBody CategoryDTO categoryDTO){
        log.info("修改分类,{}",categoryDTO);

        //调用service层进行修改
        categoryService.update(categoryDTO);

        //返回结果给前端
        return Result.success();
    }

    /**
     * 启用或禁用分类
     */
    @ApiOperation(value = "启用或禁用分类")
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable("status") Integer status,Long id){
        log.info("启用或禁用分类,{},{}",status,id);

        //调用service层进行修改
        categoryService.startOrStop(status,id);

        //返回结果给前端
        return Result.success();
    }


    /**
     * 根据type查询分类
     * @param type
     * @return
     */
    @ApiOperation(value = "根据type查询分类")
    @GetMapping("/list")
    public Result<List<Category>> list(Integer type){
        log.info("根据type查询分类,{}",type);

        //调用service层进行查询
        List<Category> categories = categoryService.list(type);

        //返回结果给前端
        return Result.success(categories);
    }
}
